package org.scirp.ijmis.dao.registration.impl;

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.scirp.ijmis.dao.base.impl.BaseDAOImpl;
import org.scirp.ijmis.dao.registration.IRegistrationDAO;
import org.scirp.ijmis.po.registration.Registration;
import org.scirp.ijmis.util.Pagination;

public class RegistrationDAOImpl extends BaseDAOImpl<Registration> implements IRegistrationDAO {

	private static final String className = RegistrationDAOImpl.class.toString();

	public List<Registration> selectAll() {

		String hql = "from Registration order by pid desc";
		return super.ExecuteQuery(hql, className + "-->selectAll");
	}

	public Long countObject() {
		return super.countObject(className + "-->selectAll");
	}

	public Registration selectById(Object id) {
		return (Registration) super.selectById(id, className + "-->selectById");
	}

	public long saveRegistration(Registration registration) {
		return super.saveObject(registration, className + "-->saveRegistration");
	}

	public boolean updateRegistration(Registration registration) {
		return super.updateObject(registration, className + "-->updateRegistration");
	}

	public boolean deleteRegistration(Long registrationId) {
		String hql = "delete Registration where pid=:registrationId";
		Map<String, Long> parameters = new HashMap<String, Long>(0);
		parameters.put("registrationId", registrationId);
		return super.ExecuteNonQuery(hql, parameters, className + "-->deleteRegistration(Long registrationId)");
	}

	public Registration selectByPaperId(Long paperId) {
		String hql = "select r from Registration r where r.paper.pid=:paperId";
		Map<String, Long> parameters = new HashMap<String, Long>();
		parameters.put("paperId", paperId);
		return (Registration) super.selectSingle(hql, parameters, className + "-->selectByPaperId");
	}

	public Long countObject(Long journalId) {
		String hql = "select count(pid) from Registration where paper.person.journal.pid=" + journalId;
		return Long.parseLong(selectSingle(hql, null, className + "-->countObject(Long journalId)").toString());
	}

	public List<Registration> selectAllOrderByDate(Pagination pagination, Long journalId) {
		String hql = "";
		if (journalId == null) {
			hql = "from Registration order by authorOperationDateTime desc";
		} else {
			hql = "from Registration where paper.person.journal.pid=" + journalId + "  order  by authorOperationDateTime desc";
		}
		if (pagination != null) {
			return super.select(hql, pagination.getCurrentPage(), pagination.getPageSize(), className + "-->selectAllOrderByDate(Pagination pagination)");
		} else {
			return super.select(hql, 0, 0, className + "-->selectAllOrderByDate(Pagination pagination)");
		}
	}

	public long selectCountByQuery(Map map) {
		if (map.size() == 0) {
			return 0;
		}
		Iterator it = map.entrySet().iterator();
		String hql = "select  count(pid) from Registration  ";
		int i = 0;
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();

			if (!key.equals("paper.pid")) {
				value = "'" + value + "'";
			}
			if (i == 0) {
				hql += " where  " + key + "=" + value;
			} else {
				hql += " and  " + key + "=" + value;
			}
			i++;
		}
		return Long.parseLong(super.selectSingle(hql, null, className + "-->selectCountByQuery(Map map )").toString());
	}

	public List<Registration> selectRegistrationByQuery(Map map, Pagination pagination) {
		if (map.size() == 0) {
			return null;
		}
		Iterator it = map.entrySet().iterator();
		String hql = "from Registration  ";
		int i = 0;
		while (it.hasNext()) {
			Map.Entry entry = (Map.Entry) it.next();
			Object key = entry.getKey();
			Object value = entry.getValue();

			if (!key.equals("paper.pid")) {
				value = "'" + value + "'";
			}
			if (i == 0) {
				hql += " where  " + key + "=" + value;
			} else {
				hql += " and  " + key + "=" + value;
			}
			i++;
		}
		hql += "    order by authorOperationDateTime desc";
		if (pagination != null) {
			return super.select(hql, pagination.getCurrentPage(), pagination.getPageSize(), className + "-->selectRegistrationByQuery(Map map,Pagination pagination )");
		} else {
			return super.select(hql, 0, 0, className + "-->selectRegistrationByQuery(Map map,Pagination pagination )");
		}
	}

	public List<Registration> selectByCriteria(Map map, String queryModel, Pagination pagination) {
		// DetachedCriteria
		// detachedCriteria=DetachedCriteria.forClass(Registration.class);
		// Iterator it = map.entrySet().iterator() ;
		// while (it.hasNext())
		// {
		// Map.Entry entry = (Map.Entry) it.next() ;
		// Object key = entry.getKey() ;
		// Object value = entry.getValue() ;
		// System.out.println(key.toString());
		// System.out.println(value);
		// System.out.println("-------------------------");
		// detachedCriteria.add(Restrictions.eq(key.toString(), value));
		// }
		//		
		// detachedCriteria.addOrder(Order.desc("authorOperationDateTime"));
		if (map.size() == 0) {
			return null;
		}
		if (pagination == null) {
			return super.selectByCriteria(null, map, queryModel, 0, 0, "authorOperationDateTime", "desc", className + "-->selectByCriteria(Map map)");
		}
		return super.selectByCriteria(null, map, queryModel, pagination.getCurrentPage(), pagination.getPageSize(), "authorOperationDateTime", "desc", className + "-->selectByCriteria(Map map)");
	}

	public long selectCountByCriteria(Map map, String queryModel) {
		if (map.size() == 0) {
			return 0;
		}
		Long count = super.countObject(null, map, queryModel, "pid", className + "-->selectCountByCriteria(Map map)");
		if (count == null) {
			return 0;
		} else {
			return count;
		}
	}

}
